import FilterBuilder from "./filterBuilder";
import request from "./request";
export default class Model {
  constructor(modelname, mode = 0) {
    this.modelname = modelname;
    this.prefix = "/admin";
    this.mode = mode;
    this.params = {};
    this.filterBuilder = new FilterBuilder();
  }
  static create(modelname, mode) {
    return new Model(modelname, mode);
  }
  page(pageNum, pageSize) {
    return this.addParam({
      page: pageNum || 1,
      limit: pageSize || 10
    });
  }
  where(query) {
    // {eq:"jack",name:'dfasd',"cc":'fasf',"dfsadf":['like','%jack%']}
    if (!query) {
      return this;
    }
    if (typeof query === "object") {
      Object.keys(query).forEach(field => {
        let value = query[field];
        let item = {};
        item.field = field;
        if (Array.isArray(value)) {
          if (value.length >= 2) {
            item.op = value[0];
            item.value = value[1];
          } else {
            item.value = value[0];
          }
        } else {
          item.value = value;
        }
        if (!item.op) {
          item.op = "eq";
        }
        this.filterBuilder.addFilter(item);
      });
    }
    return this;
  }
  toString() {
    return this.filterBuilder.toString();
  }
  pagination() {
    return this.page().select();
  }
  setParams(params) {
    this.params = params;
    return this;
  }
  query(method) {
    let url;
    if (method.indexOf("/") == -1) {
      url = this.prefix + "/" + this.modelname + "/" + method;
    } else {
      url = method;
    }
    let params = this.params;
    params.filterRules = this.filterBuilder.toString();
    Object.keys(params).forEach(key => {
      let value = params[key];
      if (typeof value == "object") {
        params[key] = JSON.stringify(value);
      }
    });
    return request({
      url,
      method: "post",
      data: params
    });
  }
  addParam(key, value) {
    if (typeof key === "object") {
      this.params = Object.assign(this.params, key);
    } else {
      this.params[key] = value;
    }
    return this;
  }
  removeParam(key) {
    if (key in this.params) {
      delete this.params[key];
    }
    return this;
  }
  resetParams() {
    this.params = {};
  }
  select() {
    let url;
    let params = this.params;
    if (this.mode == 2) {
      url = this.prefix + "/query/list";
    } else {
      url = this.prefix + "/" + this.modelname + "/list";
    }
    params.model = this.modelname;
    params.filterRules = this.filterBuilder.toString();
    return request({
      url,
      method: "post",
      data: params
    });
  }
  find() {}
  setSaveUrl(url) {
    this.saveUrl = url;
    return this;
  }
  saveAddUrl() {
    this.addUrl = url;
    return this;
  }
  save(data) {
    let url;
    if (this.saveUrl) {
      url = this.saveUrl;
    } else {
      url = this.prefix + "/" + this.modelname + "/save";
    }
    let params = this.params;
    params.filterRules = this.filterBuilder.toString();
    params = Object.assign({}, params, data);
    if (params.createTime) {
      delete params.createTime;
    }
    return request({
      url,
      method: "post",
      data: params
    });
  }
  add(data) {
    let url;
    if (this.addUrl) {
      url = this.addUrl;
    } else {
      url = this.prefix + "/" + this.modelname + "/add";
    }
    let params = this.params;
    params.filterRules = this.filterBuilder.toString();
    params = Object.assign({}, params, data);
    return request({
      url,
      method: "post",
      data: params
    });
  }
  remove() {
    let url = this.prefix + "/" + this.modelname + "/remove";
    let params = this.params;
    params.filterRules = this.filterBuilder.toString();
    return request({
      url,
      method: "post",
      data: params
    });
  }
  removeById(id) {
    let url = this.prefix + "/" + this.modelname + "/removeById";
    return request({
      url,
      method: "post",
      data: {
        ...this.params,
        id
      }
    });
  }
}
